package org.dromara.miniapp.user.controller;

import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.miniapp.user.dto.UserProfileUpdateDto;
import org.dromara.system.domain.bo.SysUserBo;
import org.dromara.system.domain.vo.SysUserVo;
import org.dromara.system.service.ISysUserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 移动端用户API控制器
 *
 * @author Claude
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/user")
public class ApiUserController extends BaseController {
    
    private final ISysUserService userService;
    
    /**
     * 获取当前登录用户信息
     */
    @GetMapping("/profile")
    public R<SysUserVo> getProfile() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        SysUserVo user = userService.selectUserById(loginUser.getUserId());
        if (ObjectUtil.isNull(user)) {
            return R.fail("用户信息不存在");
        }
        return R.ok(user);
    }
    
    /**
     * 更新用户资料
     */
    @Log(title = "用户资料更新", businessType = BusinessType.UPDATE)
    @PutMapping("/profile")
    public R<Void> updateProfile(@RequestBody @Validated UserProfileUpdateDto dto) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        Long userId = loginUser.getUserId();
        
        // 构建用户更新对象
        SysUserBo userBo = new SysUserBo();
        userBo.setUserId(userId);
        
        // 设置可更新的字段
        if (ObjectUtil.isNotEmpty(dto.getNickName())) {
            userBo.setNickName(dto.getNickName());
        }
        
        if (ObjectUtil.isNotNull(dto.getAvatar())) {
            userBo.setAvatar(dto.getAvatar());
        }
        
        if (ObjectUtil.isNotEmpty(dto.getPhonenumber())) {
            // 验证手机号唯一性
            SysUserBo checkUser = new SysUserBo();
            checkUser.setPhonenumber(dto.getPhonenumber());
            checkUser.setUserId(userId);
            if (!userService.checkPhoneUnique(checkUser)) {
                return R.fail("手机号码已存在");
            }
            userBo.setPhonenumber(dto.getPhonenumber());
        }
        
        if (ObjectUtil.isNotEmpty(dto.getSex())) {
            userBo.setSex(dto.getSex());
        }
        
        if (ObjectUtil.isNotEmpty(dto.getEmail())) {
            // 验证邮箱唯一性
            SysUserBo checkUser = new SysUserBo();
            checkUser.setEmail(dto.getEmail());
            checkUser.setUserId(userId);
            if (!userService.checkEmailUnique(checkUser)) {
                return R.fail("邮箱已存在");
            }
            userBo.setEmail(dto.getEmail());
        }
        
        // 更新用户信息
        int result = userService.updateUser(userBo);
        return result > 0 ? R.ok() : R.fail("更新失败");
    }
    
    /**
     * 更新用户头像
     */
    @Log(title = "用户头像更新", businessType = BusinessType.UPDATE)
    @PutMapping("/avatar")
    public R<Void> updateAvatar(@RequestParam("ossId") Long ossId) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        Long userId = loginUser.getUserId();
        
        SysUserBo userBo = new SysUserBo();
        userBo.setUserId(userId);
        userBo.setAvatar(ossId);
        
        int result = userService.updateUser(userBo);
        return result > 0 ? R.ok() : R.fail("头像更新失败");
    }
    
    /**
     * 更新用户昵称
     */
    @Log(title = "用户昵称更新", businessType = BusinessType.UPDATE)
    @PutMapping("/nickname")
    public R<Void> updateNickname(@RequestParam("nickName") String nickName) {
        if (ObjectUtil.isEmpty(nickName) || nickName.length() > 30) {
            return R.fail("昵称不能为空且长度不能超过30个字符");
        }
        
        LoginUser loginUser = LoginHelper.getLoginUser();
        Long userId = loginUser.getUserId();
        
        SysUserBo userBo = new SysUserBo();
        userBo.setUserId(userId);
        userBo.setNickName(nickName);
        
        int result = userService.updateUser(userBo);
        return result > 0 ? R.ok() : R.fail("昵称更新失败");
    }
}